package com.nanhai.buddhist.college.module.collegebase.controller.admin.rewardpunishrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.rewardpunishrecord.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.rewardpunishrecord.RewardPunishRecordDO;
import com.nanhai.buddhist.college.module.collegebase.convert.rewardpunishrecord.RewardPunishRecordConvert;
import com.nanhai.buddhist.college.module.collegebase.service.rewardpunishrecord.RewardPunishRecordService;

@Api(tags = "管理后台 - 奖罚管理")
@RestController
@RequestMapping("/collegebase/reward-punish-record")
@Validated
public class RewardPunishRecordController {

    @Resource
    private RewardPunishRecordService rewardPunishRecordService;

    @PostMapping("/create")
    @ApiOperation("创建奖罚管理")
    public CommonResult<String> createRewardPunishRecord(@Valid @RequestBody RewardPunishRecordCreateReqVO createReqVO) {
        return success(rewardPunishRecordService.createRewardPunishRecord(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新奖罚管理")
    public CommonResult<Boolean> updateRewardPunishRecord(@Valid @RequestBody RewardPunishRecordUpdateReqVO updateReqVO) {
        rewardPunishRecordService.updateRewardPunishRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除奖罚管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:reward-punish-record:delete')")
    public CommonResult<Boolean> deleteRewardPunishRecord(@RequestParam("id") String id) {
        rewardPunishRecordService.deleteRewardPunishRecord(id);
        return success(true);
    }

    @DeleteMapping("/cancel")
    @ApiOperation("取消奖罚记录")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataTypeClass = String.class)
    public CommonResult<Boolean> cancelRewardPunishRecord(@RequestParam("id") String id) {
        rewardPunishRecordService.cancelRewardPunishRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得奖罚管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<RewardPunishRecordRespVO> getRewardPunishRecord(@RequestParam("id") String id) {
        RewardPunishRecordDO rewardPunishRecord = rewardPunishRecordService.getRewardPunishRecord(id);
        return success(RewardPunishRecordConvert.INSTANCE.convert(rewardPunishRecord));
    }

    @GetMapping("/list")
    @ApiOperation("获得奖罚管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<RewardPunishRecordRespVO>> getRewardPunishRecordList(@RequestParam("ids") Collection<String> ids) {
        List<RewardPunishRecordDO> list = rewardPunishRecordService.getRewardPunishRecordList(ids);
        return success(RewardPunishRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得奖罚管理分页")
    public CommonResult<PageResult<RewardPunishRecordRespVO>> getRewardPunishRecordPage(@Valid RewardPunishRecordPageReqVO pageVO) {
        PageResult<RewardPunishRecordDO> pageResult = rewardPunishRecordService.getRewardPunishRecordPage(pageVO);
        return success(RewardPunishRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出奖罚管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportRewardPunishRecordExcel(@Valid RewardPunishRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RewardPunishRecordDO> list = rewardPunishRecordService.getRewardPunishRecordList(exportReqVO);
        // 导出 Excel
        List<RewardPunishRecordExcelVO> datas = RewardPunishRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "奖罚管理.xls", "数据", RewardPunishRecordExcelVO.class, datas);
    }

}
